/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.dao;

//~--- non-JDK imports --------------------------------------------------------
import com.myapp.struts.actionform.AuthenticationFormBean;

import org.apache.struts.action.ActionForm;

//~--- JDK imports ------------------------------------------------------------

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pavan
 */
public class AuthenticationDao {

    private AuthenticationFormBean afb;
    private ResultSet authres;
    private String checkUserName;
    private Connect con;
    private Connection connection;
    private PreparedStatement pstmt;

    public AuthenticationDao() {
        con = new Connect();
        afb = new AuthenticationFormBean();
    }

    public String check(ActionForm form) {

        /** Type cast ActionForm variable to FormBean */
        afb = (AuthenticationFormBean) form;

        /** Take Database Connection */
        connection = con.mySql();

        if (connection != null) {
            try {
                checkUserName = "SELECT role_name,u.username FROM users u,user_roles r " + "where u.userid=? and u.password=? and " + " u.userid=r.userid";
                pstmt = connection.prepareStatement(checkUserName);
                pstmt.setString(1, afb.getUserName());
                pstmt.setString(2, afb.getPassword());
                authres = pstmt.executeQuery();

                if (authres.next() == true) {
                    return ((String) authres.getString(1) + "," + (String) authres.getString(2) + ",");
                }
            } catch (SQLException ex) {
                Logger.getLogger(AuthenticationDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            return null;
        }

        return null;
    }
}
